<?php

namespace App\Http\Controllers;

//获取一般任务列表
use App\Common\Sql;

class TaskList extends Base
{

    public function logic()
    {
        if (!isset($this->param['uid'])) {
            $this->returnError(1, "please input uid");
        }
        $uid      = $this->param['uid'];
        $data     = array();
        $cur_time = time();
        $row = Sql::select('t_task_conf',[['status',1]]);
        foreach ($row as $info) {
            $task_info  = array();
            $begin_time = strtotime($info['begin_time']);
            $end_time   = strtotime($info['end_time']);
            //检查是否在活动时间内
            if ($cur_time < $begin_time || $cur_time > $end_time) {
                continue;
            }
            $taskid                  = (int)$info['id'];
            $task_info['task_id']    = $taskid;
            $task_info['task_name']  = $info['name'];
            $task_info['limit']      = (int)$info['limit'];
            $task_info['begin_time'] = $info['begin_time'];
            $task_info['end_time']   = $info['end_time'];
            //从redis里获取用户任务完成状态
            list($unclaimed, $finished) = $this->get_user_task_status($uid, $taskid);
            $task_info['unclaimed'] = $unclaimed;
            $task_info['finished']  = $finished;
            $reward                 = $info['reward'];
            //解析任务奖品
            $rewards = explode('|', $reward);
            foreach ($rewards as $items) {
                $item_num = explode('=', $items);
                if (count($item_num) != 2) {
                    continue;
                }
                $item['itemid']             = (int)$item_num[0];
                $item['num']                = (int)$item_num[1];
                $task_info['reward_list'][] = $item;
            }
            $data['task_list'][] = $task_info;
        }
        $data['hero_count'] = $this->get_user_item_num($uid, 1001);
        $data['uid']        = $uid;
        return $this->returnData($data);
    }

}

